let mockUsers = require("../mock/mockUser");
const { UserModel } = require("../models");

async function addUsers(users) { 
  let data = await UserModel.insertMany(users);
  return data;
}

// addUsers(mockUsers);

/**
 * 查询所有用户信息
 * @returns 用户信息的数组
 */
async function getAllUsers() { 
  let result = await UserModel.find();
  return result;
}

/**
 * 返回所有用户数据的个数
 * @returns 所有用户数据的数量
 */
async function getAllUsersCount() { 
  let data = await UserModel.find().count();
  return data;
}

async function list(filter,page=1,limit=10) { 
  let result = await UserModel
    .find(filter)
    .skip((page - 1) * limit)
    .limit(limit);
  return result;
}

async function count(filter) { 
  let data = await UserModel.find(filter).count();
  return data;
}

async function deleteById(id) { 
  let data = await UserModel.deleteOne({ "_id": id });
  return data;
}

async function add(user) {
  let data = await UserModel.create(user);
  return data;
}

async function login(loginId, loginPwd) { 
  let data = await UserModel.findOne({ loginId, loginPwd });
  return data;
}
async function findOne(_id) { 
  let data = await UserModel.findById(_id);
  return data;
}

module.exports.getAllUsers = getAllUsers;
module.exports.getAllUsersCount = getAllUsersCount;
module.exports.list = list;
module.exports.count = count;
module.exports.deleteById = deleteById;
module.exports.add = add;
module.exports.login = login;
module.exports.findOne = findOne;